package edu.bit.water.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.EntityEvaWsrcInfoDao;
import edu.bit.water.dao.LabelEvaWsrcInfoDao;
import edu.bit.water.entity.LabelEvaWsrcInfo;

@Service
public class LabelEvaWsrcInfoService {

	@Autowired
	private LabelEvaWsrcInfoDao labelEvaWsrcInfoDao;
	@Autowired
	private EntityEvaWsrcInfoDao entityEvaWsrcInfoDao;

	/**
	 * 
	 * @param labelEvaWsrcInfo 标签实体
	 * @return 成功失败信息放入map中
	 */
	public Map<String, Object> insertLabelEvaWsrcInfo(LabelEvaWsrcInfo labelEvaWsrcInfo) {
		Map<String, Object> modelMap = new HashMap<>();

		try {
			labelEvaWsrcInfoDao.insertLabelEvaWsrcInfo(labelEvaWsrcInfo);
			modelMap.put("status", "success");
		} catch (DuplicateKeyException e) {
			if (e.getMessage().contains("INFO_LAB")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "创建日期已存在，请重新输入");
			}
		}
		return modelMap;
	}

	/**
	 * 
	 * @param listId 序号组
	 * @return 成功返回1，失败返回0
	 */
	public int deleteLabelEvaWsrcInfo(List<Integer> listId) {
		try {
			for (Integer id : listId) {
				entityEvaWsrcInfoDao.deleteEntityEvaWsrcInfoByLabelId(id);// 组号为
				labelEvaWsrcInfoDao.deleteLabelEvaWsrcInfo(id);
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			// 最终共同操作
		}
	}

	/**
	 * 选出所有数据
	 * 
	 * @return
	 */
	public ArrayList<LabelEvaWsrcInfo> queryLabelEvaWsrcInfo() {
		ArrayList<LabelEvaWsrcInfo> list = labelEvaWsrcInfoDao.queryLabelEvaWsrcInfo();
		return list;
	}

	/**
	 * 更新描述字段
	 * 
	 * @param labelEvaWsrcInfo 更新数据
	 * @return 成功返回1，失败返回0
	 */
	public int updateLabelEvaWsrcInfo(LabelEvaWsrcInfo labelEvaWsrcInfo) {
		try {
			labelEvaWsrcInfoDao.updateLabelEvaWsrcInfo(labelEvaWsrcInfo);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			// 最终共同操作
		}
	}
}
